package net.zrnecko.km.entity;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@NamedQueries( {
		@NamedQuery(name = "findAllTournaments", query = "select from Tournament t"),
		@NamedQuery(name = "findTournamentByTourId", query = "select from Tournament t where t.tournamentId = :tournamentId") })
public class Tournament implements Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	private int tournamentId;

	@Temporal(value = TemporalType.DATE)
	private Date date;

	public Tournament() {
	}

	public Tournament(int tournamentId, Date date) {
		this.setTournamentId(tournamentId);
		this.setDate(date);
	}
	public Tournament(int tournamentId, String dateString)
			throws ParseException {
		if (dateString.length() != 10) throw new ParseException("Date too long!", 0);
		if (!dateString.matches("20[0-9]{2}-[0-1][0-9]-[0-3][0-9]")) throw new ParseException("Invalid date!", 0);
		this.setTournamentId(tournamentId);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		this.setDate(sdf.parse(dateString));

	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public int getTournamentId() {
		return tournamentId;
	}

	public void setTournamentId(int tournamentId) {
		this.tournamentId = tournamentId;
	}

}
